<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class PointGiftSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'CourseSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $courses = $this->fetchAll('SELECT * FROM kg_course');

        $rows = [];
        for ($i = 0; $i < 25; $i++) {
            $course = Arr::rand($courses);
            $attrs = [
                'id' => $course['id'],
                'title' => $course['title'],
                'price' => $faker->randomFloat(2, 50, 200),
            ];

            $rows[] = [
                'name' => $course['title'],
                'cover' => $course['cover'],
                'details' => '',
                'attrs' => json_encode($attrs, JSON_UNESCAPED_UNICODE),
                'type' => 1,
                'stock' => 999,
                'point' => 9900,
                'published' => 1,
                'redeem_limit' => 1,
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }

        $this->table('kg_point_gift')->insert($rows)->saveData();
    }
}
